﻿
@section scripts{
    <script type="text/javascript">
        var bootstrap = function ($, ibpm) {
            "use strict";
            var selectedUserId;
            var selectedUserName;
            var refreshGirdData;
            var page = {
                init: function () {
                    $("#En_RoleConfig").on('click', page.RoleConfig);
                    $("#btn_Search").on('click', function () {
                        page.search();
                    });
                    $('#btn_Refresh').on('click', function () {
                        location.reload();
                    });
                    page.initGrid();
                    page.bind();
                },
                bind: function () {
                    //公司信息
                    $('#using_tree').ibpm_tree({
                        url: top.$.rootUrl + '/Org/Power/GetOrgTree',
                        icons: true,
                        hasSearch: true,
                        isLeaf: false,
                        multiple: false,
                        nodeClick: function (item) {
                            ibpm.httpAsyncPost(top.$.rootUrl + "/Org/Power/GetDeptCom", { id: item }, function (json) {
                                $('#CurrCompanyID').val(json.data.ComId);
                                $('#CurrCompanyName').val(json.data.ComName);
                                $('#CurrDeptID').val(json.data.DeptId);
                                $('#CurrDeptName').val(json.data.DeptName);
                                page.search();
                            });
                        }
                    });
                },
                initGrid: function () {
                    //绑定列表数据
                    $('#gridtable').jfGrid({
                        url: top.$.rootUrl + '/Org/Power/GetPageList',
                        headData: [
                            { label: '用户', name: 'FullName', index: "FullName", width: 120, align: 'left' },
                            { label: '工号', name: 'JobNumber', width: 120, align: 'left' },
                            { label: '账户', name: 'Account', width: 120, align: 'left' },
                            { label: '公司', name: 'CompanyName', width: 120, align: 'left' },
                            { label: '部门', name: 'DepartmentName', width: 120, align: 'left' },
                            { label: '应用', name: 'ApplyKey', width: 120, align: 'left' }
                        ],
                        mainId: 'UserId',
                        sidx: 'JobNumber',    //排序
                        sord: 'desc',
                        isPage: true,
                        rows: 16,
                        isSubGrid: true,    // 是否有子表单
                        onSelectRow: function (rowdata) {
                            //中文编码解码：encodeURI(txt);对应decodeURI(etxt)
                            //字符加密解密：escape(txt);对应unescape(etxt)
                            selectedUserId = rowdata.UserId;  //Guid不用编码
                            selectedUserName = escape(rowdata.FullName);
                        },
                        subGridExpanded: function (subid, rowdata) {
                            $('#' + subid).jfGrid({
                                url: top.$.rootUrl + '/Org/Power/GetRoleList',
                                headData: [
                                    { label: "角色名称", name: "RoleName", width: 200, align: "left" },
                                    { label: "角色编码", name: "RoleCode", width: 100, align: "center" },
                                    {
                                        label: "角色授权范围", name: "btn", width: 160, align: "center",
                                        formatter: function (value, row, op, $cell) {
                                            if (!!row.RoleId && row.RoleId != '') {
                                                $cell.on('click', function () {
                                                    page.RoleWizard(row.RoleId);
                                                    return false;
                                                });
                                                return '<span class=\"label label-info \" style=\"cursor: pointer;\">授权范围</span>';
                                            }
                                            else
                                                return '';
                                        }
                                    },
                                ],
                                mainId: 'RoleId',
                                onSelectRow: function (rowsubdata) {
                                    //alert(rowsubdata.RoleId); //选择行时触发
                                },
                            }).jfGridSet('reload', { param: { UID: rowdata.UserId } });
                        }
                    });

                    //绑定角色下的模块列表
                    $('#gridModule').jfGrid({
                        url: top.$.rootUrl + '/Org/Power/SelectRoleModule',
                        headData: [
                            { label: "模块名称", name: "ModuleName", width: 200, align: "left" },
                            { label: "模块编码", name: "ModuleCode", width: 100, align: "center" },
                            {
                                label: "模块图标", name: "ModuleIcon", width: 80, align: "center",
                                formatter: function (cellvalue) {
                                    return "<i class=\"" + cellvalue + "\"></i>";
                                }
                            },
                            { label: "资源名称", name: "ResourceTypeName", width: 100, align: "center" },
                            {
                                label: "是否数据权限", name: "btn", width: 120, align: "right",
                                formatter: function (value, row, op, $cell) {
                                    if (!!row.ResourceTypeId && row.HasDataPower) {
                                        $cell.on('click', function () {
                                            page.RoleDataPower(row.ResourceTypeId, row.RoleId, row.UserId);
                                            return false;
                                        });
                                        return '<span class=\"label label-danger \" style=\"cursor: pointer;\">数据权限</span>';
                                    }
                                    else
                                        return '';
                                }
                            },
                        ],
                        mainId: 'ModuleId',
                        parentId: 'ParentId',
                        isTree: true,
                        rows: 9900,
                    });
                },
                RoleConfig: function () {
                    if (ibpm.checkrow(selectedUserId, '请选择用户')) {
                        ibpm.layerForm({
                            id: 'RoleConfigLayer',
                            title: '配置角色',
                            url: top.$.rootUrl + '/Org/Power/GetSelectLayer?UID=' + selectedUserId + "&UName=" + selectedUserName,
                            width: 800,
                            height: 500,
                            callBack: function (id) {
                                page.search();  //刷新列表
                                return true;
                            }
                        });
                    }
                },
                RoleWizard: function (RoleId) {
                    if (ibpm.checkrow(RoleId, '请选择角色')) {
                        if (ibpm.checkrow(selectedUserId, '请点亮用户')) {
                            $('#gridModule').jfGridSet('reload', { param: { RID: RoleId, UID: selectedUserId } });
                            //显示弹层
                            $('#RoleNode').modal("show");
                        }
                    }
                },
                RoleDataPower: function (RTId, RId, UId) {
                    
                },

                search: function (param) {
                    selectedUserId = '';
                    selectedUserName = '';

                    param = param || {};
                    param.companyId = $('#CurrCompanyID').val();
                    param.deptId = $('#CurrDeptID').val();
                    param.keyword = $('#txt_Keyword').val();
                    $('#gridtable').jfGridSet('reload', param);
                }
            };
            page.init();
            refreshGirdData = function (data) {
                alert(data);
            };
        };
    </script>
}

<div class="row">
    <div class="step-pane">
        <div class="row">
            <div class="col-md-5">
                <div class="input-group">
                    <input type="hidden" id="CurrCompanyID" value="" />
                    <input type="hidden" id="CurrCompanyName" value="" />
                    <input type="hidden" id="CurrDeptID" value="" />
                    <input type="hidden" id="CurrDeptName" value="" />
                    <input id="txt_Keyword" type="text" class="form-control-sm form-control" placeholder="请输入要查询的关键字" />
                    <span id="btn_Search" class="input-group-addon btn btn-primary"><i class="fa fa-search"></i>&nbsp;搜索</span>
                    <span id="btn_Refresh" class="input-group-addon btn btn-info"><i class="fa fa-refresh"></i>&nbsp;刷新</span>
                </div>
            </div>
            <div class="col-md-7">
                <div class="btn-group btn-group-sm" style="float:right;">
                    <a id="En_RoleConfig" class="btn btn-default"><i class="fa fa-address-book"></i>&nbsp;配置角色</a>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="row">
    <div class="step-pane">
        <div class="row" style="margin: 5px;">
            <div class="col-lg-4">
                <div class="step-pane">
                    <h3 class="modal-title">组织架构</h3>
                    <div id="using_tree"></div>
                </div>
            </div>
            <div class="col-lg-8">
                <div id="gridtable"></div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="RoleNode" tabindex="-11" role="dialog" aria-labelledby="RoleNode" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content animated bounceInRight">
            <div class="modal-header">
                <h3 class="modal-title">分配模块</h3>
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="row" style="margin: 5px;">
                    <div id="gridModule"></div>
                </div>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>
